package cn.alcatraz.user.service.impl;

import cn.alcatraz.basic.constant.BaseConstant;
import cn.alcatraz.basic.dto.SmsCodeDto;
import cn.alcatraz.basic.exception.BusinessException;
import cn.alcatraz.basic.utils.MD5Utils;
import cn.alcatraz.basic.utils.StrUtils;
import cn.alcatraz.user.domain.Logininfo;
import cn.alcatraz.user.domain.User;
import cn.alcatraz.user.mapper.LogininfoMapper;
import cn.alcatraz.user.mapper.UserMapper;
import cn.alcatraz.user.service.IUserService;
import cn.alcatraz.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author alcatraz
 * @since 2022-07-06
 */

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void registerPhone(SmsCodeDto smsCodeDto) {

        String phone = smsCodeDto.getPhone();

        String phoneCodeValue = smsCodeDto.getPhoneCodeValue();

        String password = smsCodeDto.getPassword();

        String confirmPassword = smsCodeDto.getConfirmPassword();

        String phoneCodeKey = BaseConstant.VerifyCodeConstant.BUSINESS_REGISTER_KEY + phone;

        Object phoneCodeRedisValue = redisTemplate.opsForValue().get(phoneCodeKey);
        //为空校验
        if(StringUtils.isEmpty(phone)){
            throw new BusinessException("手机号不能为空");
        }
        //判断手机号是否和获得验证码的一致
        if(phoneCodeRedisValue == null){
            throw new BusinessException("验证码已失效,请重新获取");
        }
        //判断手机验证码是否正确
        String phoneCode = phoneCodeRedisValue.toString().split(":")[1];
        if(!phoneCode.equalsIgnoreCase(phoneCodeValue)){
            throw new BusinessException("手机验证码错误,请重新输入");
        }
        //输入的两次密码是否一致
        if(!password.equals(confirmPassword)){
            throw new BusinessException("前后密码不一致,请重新输入");
        }
        //判断用户是否已经注册
        User user = userMapper.loadByPhone(phone);
        if(user != null){
            throw new BusinessException("用户已被注册");
        }
        //初始化User表.对user赋值操作
        user = smsCodeDto2User(smsCodeDto);
        //保存user
        userMapper.save(user);
        //将user表字段保存到logininfo
        Logininfo logininfo = user2Logininfo(user);
        logininfoMapper.save(logininfo);
        //将logininfoID存到user
        user.setLogininfoId(logininfo.getId());
        userMapper.update(user);

    }


    /**
     * @Title: smsCodeDto2User
     * @Description: 将smsCodeDto的值赋值给user
     * @Author: Alcatraz
     * @Version: 1.0
     * @Date:  2022/7/6 18:40
     * @Parameters: [smsCodeDto]
     * @Return cn.alcatraz.user.domain.User
     */
    private User smsCodeDto2User(SmsCodeDto smsCodeDto) {
        String phone = smsCodeDto.getPhone();
        User user = new User();
        user.setUsername(phone);
        user.setPhone(phone);
        String salt = StrUtils.getComplexRandomString(32);
        String saltPassword = MD5Utils.encrypByMd5(salt + smsCodeDto.getPassword());
        user.setPassword(saltPassword);
        user.setSalt(salt);
        return user;
    }

    /**
    * @Title: emailCodeDto2User
    * @Description: 把邮箱值给user
    * @Author: Alcatraz
    * @Version: 1.0
    * @Date:  2022/7/8 19:12
    * @Parameters: [smsCodeDto]
    * @Return cn.alcatraz.user.domain.User
    */
    private User emailCodeDto2User(SmsCodeDto smsCodeDto) {
        String email = smsCodeDto.getEmail();
        User user = new User();
        user.setUsername(email);
        user.setEmail(email);
        String salt = StrUtils.getComplexRandomString(32);
        String saltPassword = MD5Utils.encrypByMd5(salt + smsCodeDto.getPassword());
        user.setPassword(saltPassword);
        user.setSalt(salt);
        return user;
    }

    /**
     * @Title: user2Logininfo
     * @Description: 将user的值赋值给logininfo
     * @Author: Alcatraz
     * @Version: 1.0
     * @Date:  2022/7/6 18:40
     * @Parameters: [user]
     * @Return cn.alcatraz.user.domain.Logininfo
     */
    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user, logininfo);
        logininfo.setType(1);
        return logininfo;
    }


    @Override
    public void registerEmail(SmsCodeDto smsCodeDto) {

        String email = smsCodeDto.getEmail();

        String emailCodeValue = smsCodeDto.getEmailCodeValue();

        String password = smsCodeDto.getPassword();

        String confirmPassword = smsCodeDto.getConfirmPassword();

        String emailCodeKey = BaseConstant.VerifyCodeConstant.BUSINESS_REGISTER_KEY + email;

        Object emailCodeRedisValue = redisTemplate.opsForValue().get(emailCodeKey);
        //为空校验
        if(StringUtils.isEmpty(email)){
            throw new BusinessException("邮箱不能为空");
        }
        //判断邮箱是否和获得验证码的一致
        if(emailCodeRedisValue == null){
            throw new BusinessException("验证码已失效,请重新获取");
        }
        //判断手机验证码是否正确
        String emailCode = emailCodeRedisValue.toString().split(":")[1];
        if(!emailCode.equalsIgnoreCase(emailCodeValue)){
            throw new BusinessException("邮箱验证码错误,请重新输入");
        }
        //输入的两次密码是否一致
        if(!password.equals(confirmPassword)){
            throw new BusinessException("前后密码不一致,请重新输入");
        }
        //判断用户是否已经注册
        User user = userMapper.loadByEmail(email);
        if(user != null){
            throw new BusinessException("用户已被注册");
        }
        //初始化User表.对user赋值操作
        user = emailCodeDto2User(smsCodeDto);
        //保存user
        userMapper.save(user);
        //将user表字段保存到logininfo
        Logininfo logininfo = user2Logininfo(user);
        logininfoMapper.save(logininfo);
        //将logininfoID存到user
        user.setLogininfoId(logininfo.getId());
        userMapper.update(user);

    }

    @Override
    public User loadByLogininfoId(Long id) {
        return userMapper.loadByLogininfoId(id);
    }

}
